package com.supertange.miniapp.wepet.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

public class ConvertPath {
    private static final Logger logger = LoggerFactory.getLogger(ConvertPath.class);

    /**
     * 在磁盘上创建文件夹
     * @param filePath 文件路径
     * @return
     */
    public static String getPath(String filePath){
        //获取根目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            logger.info("文件夹路径未找到:"+e.getLocalizedMessage());
        }
        //如果上传目录为/static/images/upload/，则可以如下获取：
        //File upload = new File(path.getAbsolutePath(),"/static/images/upload/");
        File file = new File(path.getAbsolutePath(),filePath);
        if(!file.exists()) file.mkdirs();
        //在开发测试模式时，得到的地址为：{项目跟目录}/target/static/images/upload/
        //在打包成jar正式发布时，得到的地址为：{发布jar包目录}/static/images/upload/
        return file.getAbsolutePath();
    }
}
